﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.uc_control
{
    public partial class Login : UserControl, GUI.uc_control.ucInterface
    {
        Action<GUI.uc_control.ucEventInterface> _CallBack;

        public Login()
        {
            InitializeComponent();
            _CallBack = null;
        }

        public Login(Action<GUI.uc_control.ucEventInterface> callback)
        {
            InitializeComponent();
            _CallBack = callback;
        }

        public String GetName()
        {
            return "LOGIN";
        }

        public int GetUCID()
        {
            return GUI.uc_control.ucID.UCID_LOGIN;
        }

        public bool NeedToSave() { return false; }
        public int SaveChange() { return 0; }

        private void button_Login_Click(object sender, EventArgs e)
        {
            String username = this.textbox_username.Text;
            String userpass = this.textbox_password.Text;
            int userRole = GUI.uc_control.uc_event.ucLoginEvent.INVALID_ROLE;
            int userID = GUI.gui_dto.User.INVALID_USER_ID;

            if (username == null || username == String.Empty)
            {
                this.label_valid_username.Visible = true;
                return;
            }

            if (userpass == null || userpass == String.Empty)
            {
                this.label_valid_password.Visible = true;
                return;
            }

            if (username == "admin" && userpass == "admin")
            {
                userRole = GUI.uc_control.uc_event.ucLoginEvent.ADMIN_ROLE;
                userID = 1; // test
            }
            else if (username == "seller" && userpass == "seller")
            {
                userRole = GUI.uc_control.uc_event.ucLoginEvent.SELLER_ROLE;
                userID = 2; // test
            }

            if (userRole == GUI.uc_control.uc_event.ucLoginEvent.INVALID_ROLE)
            {
                this.label_err_msg.Text = "người dùng không hợp lệ!!";
                this.label_err_msg.Visible = true;
                return;
            }

            if (userID == GUI.gui_dto.Role.INVALID_USER_ID)
            {
                this.label_err_msg.Text = "người dùng không hợp lệ!!";
                this.label_err_msg.Visible = true;
                return;
            }

            if (_CallBack != null)
            {
                _CallBack(new GUI.uc_control.uc_event.ucLoginEvent(userRole, userID));
            }
        }
    }
}
